package jsyf.oa.service.capital;

import com.alibaba.fastjson.JSONObject;
import jsyf.oa.dao.capital.BankCardDao;
import jsyf.oa.dao.system.CompanyDao;
import jsyf.oa.dao.system.WechatShareDao;
import jsyf.oa.facade.capital.CapitalFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class CapitalService implements CapitalFacade {

    @Autowired
    private BankCardDao bankCardDao;
    @Autowired
    private CompanyDao companyDao;

    @Override
    public List<JSONObject> getCapitalTotal(String jsonData, String date) {

        //所有公司
        List<JSONObject> companyList = companyDao.getJSONListByCondition(" AND DEL_FLAG=0  ORDER BY SORT_NO ", null);
        List<JSONObject> list = new ArrayList<>();
        for (Integer i = 0; i < companyList.size(); i++) {
            String companyId = companyList.get(i).get("id").toString();
            String companyName = companyList.get(i).get("name").toString();
            JSONObject jsonObject = bankCardDao.getCapital(companyId, date,getYesterDay(date));
            JSONObject debit = bankCardDao.getDebits(companyId,date);
            String currentDebits = debit.getString("currentDebits");
            JSONObject yestdayDebit = bankCardDao.getDebits(companyId,getYesterDay(date));
            String yestdayDebits = yestdayDebit.getString("currentDebits");
            jsonObject.put("currentDebits",currentDebits);
            jsonObject.put("yestdayDebits",yestdayDebits);
            jsonObject.put("companyId",companyId);
            jsonObject.put("companyName",companyName);
            list.add(jsonObject);
        }
        return list;
    }

    @Override
    public JSONObject getFundsTotal() {

        Date date = new Date();
        String result = null;
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
        result = sdf.format(date);
        JSONObject jsonObject = bankCardDao.getFundsTotal(getYesterDay(result));
        return jsonObject;
    }

    public String getYesterDay(String dateString){
        //通过传过来的时间算出前一天
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Date dayCurrent = null;
        try {
            dayCurrent = format1.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dayCurrent);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        dayCurrent = calendar.getTime();
        return sdf.format(dayCurrent);
    }
}
